#
# @lc app=leetcode.cn id=98 lang=python3
#
# [98] 验证二叉搜索树
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


class Solution:
    def isValidBST(self, root: TreeNode) -> bool:
        """
        中序遍历：左->结点->右  对于二叉搜索树，每个元素都应该比下一个元素小
        """
        stack, inorder = [], float('-inf')

        while stack or root:
            while root:
                stack.append(root)
                root = root.left
            root = stack.pop()
            # inorder是前一个元素
            if root.val <= inorder:
                return False
            inorder = root.val
            root = root.right
        # @lc code=end
